package com.zkthink.ceres.app.chlOrder.controller;

import cn.hutool.extra.servlet.ServletUtil;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.partnerpayments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.payments.jsapi.model.Amount;
import com.wechat.pay.java.service.payments.jsapi.model.Payer;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse;
import com.zkthink.base.R;
import com.zkthink.ceres.applet.order.service.AppletServerOrderService;
import com.zkthink.ceres.order.vo.MoneyReturnVO;
import com.zkthink.ceres.store.dto.HeXiaoDto;
import com.zkthink.ceres.store.dto.StoreWithdrawHisSaveDTO;
import com.zkthink.clh.order.dto.*;

import com.zkthink.clh.order.entity.ChlLbt;
import com.zkthink.clh.order.entity.ChlOrderRefund;
import com.zkthink.clh.order.vo.ChlCountinuePayVo;
import com.zkthink.clh.order.vo.ChlPlaceOrderVO;
import com.zkthink.context.BaseContextHandler;
import com.zkthink.zhl.order.service.ChlCouponsService;
import com.zkthink.zhl.order.service.ChlOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName OrderController
 * @Description: OrderController
 * @Author ck
 * @Date 2021/11/30
 * @Version V1.0
 **/
@Slf4j
@Validated
@RestController
@RequestMapping("/chlOrder")
@Api(value = "车龙惠订单接口(充值vip购买消费金 店铺)", tags = "订单接口")
@AllArgsConstructor
@Transactional
public class ChlOrderController {
    @Autowired
    private ChlOrderService chlOrderService;
    @Autowired
    private ChlCouponsService couponsService;

    @Autowired
    private AppletServerOrderService appletServerOrderService;

    public static RSAAutoCertificateConfig config = null ;
    public static JsapiServiceExtension service = null ;

    @ApiOperation("下单")
    @PostMapping("/placeOrder")
    public R<ChlPlaceOrderVO> placeOrder(HttpServletRequest request, @RequestBody ChlPlaceOrderDTO placeOrderDTO) {
        Long mid = BaseContextHandler.getUserId();
        placeOrderDTO.setMid(mid);
        String ip = ServletUtil.getClientIP(request);
        placeOrderDTO.setIp(ip);
        return chlOrderService.placeOrder(placeOrderDTO);
    }

    @ApiOperation("继续付款")
    @PostMapping("/continuePay")
    public R<ChlCountinuePayVo> continuePay(HttpServletRequest request, @RequestBody ChlContinuePayOrderDTO continuePayOrderDTO) {
//        Long mid = BaseContextHandler.getUserId();
//        continuePayOrderDTO.setMid(mid);

        String ip = ServletUtil.getClientIP(request);
        continuePayOrderDTO.setIp(ip);
        return chlOrderService.continuePay(continuePayOrderDTO);
    }

    @ApiOperation("申请退款")
    @PostMapping("/refund")
    public R<MoneyReturnVO> refund(@RequestBody ChlOrderRefund refund) {
        Long mid = BaseContextHandler.getUserId();
        refund.setMid(mid);
        return chlOrderService.refund(refund);
    }

    @ApiOperation(value = "提现申请", notes = "提现申请")
    @PostMapping(value = "/withdraw")
    public R withdraw(@RequestBody StoreWithdrawHisSaveDTO dto) {
        return chlOrderService.withdraw(dto);
    }

    @ApiOperation("核销")
    @PostMapping("/examine")
    public R examine(@RequestBody HeXiaoDto dto) {

        R r = appletServerOrderService.examine(dto);
        if (r.getCode() == R.OPERATION_EX_CODE) {
            return chlOrderService.examine(dto);
        }
        return r;
    }

    @ApiOperation("我的订单")
    @GetMapping("/myOeder/{mid}")
    public R myOeder(@PathVariable("mid") Long mid) {
        return chlOrderService.myOeder(mid);
    }

    @ApiOperation("微信查询订单信息")
    @GetMapping("/wxSelectOrder/{id}")
    public R selectOrder(@PathVariable("id") String id) {
        return chlOrderService.wxSelectOrder(id);
    }

    @ApiOperation("删除我的订单")
    @GetMapping("/deleteOeder/{id}")
    public R deleteOeder(@PathVariable("id") Long id) {
        return chlOrderService.deleteOeder(id);
    }

    @ApiOperation("订单消费金额")
    @PostMapping("/xiadan")
    public R xiadan(@RequestBody XiadanDto dto) {
        return chlOrderService.xiadan(dto);
    }

    @ApiOperation("订单详情")
    @GetMapping("/getOeder/{id}")
    public R getOeder(@PathVariable("id") Long id) {
        return chlOrderService.getOeder(id);
    }

    @ApiOperation("我的钱包详情")
    @PostMapping("/myqianbao")
    public R myqianbao(@RequestBody MyqianbaoDto dto) {

        return chlOrderService.myqianbao(dto);
    }

    @ApiOperation("app轮播图")
    @GetMapping("/applist")
    public R applist() {
        return R.success(chlOrderService.applist());
    }

    @ApiOperation("app视频介绍")
    @GetMapping("/videoList")
    public R videoList() {
        return R.success(chlOrderService.selectVideo());
    }

    @ApiOperation("app获取消费金")
    @GetMapping("/appCoupons")
    public R appCoupons() {
        return R.success(couponsService.appCoupons());
    }

    @ApiOperation("app获取Vip价格")
    @GetMapping("/getVip")
    public R getVip() {
        return R.success(couponsService.getVip());
    }

    @ApiOperation("app获取店铺入住费")
    @GetMapping("/getRuzhufei")
    public R getRuzhufei() {
        return R.success(couponsService.getRuzhufei());
    }

    @ApiOperation("店铺提现账单查询")
    @PostMapping("/storeTxBill")
    public R storeTxBill(@RequestBody StoreBillDto dto) {
        return chlOrderService.storeTxBill(dto);
    }

    @ApiOperation("店铺账单查询")
    @PostMapping("/storeBill")
    public R storeBill(@RequestBody StoreBillDto dto) {
        return chlOrderService.storeBill(dto);
    }


}
